package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.record.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;
import sun.security.util.AuthResources_it;

import java.util.function.DoubleToIntFunction;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/4/17 11:53
 * @Version 1.0
 */

@Service
public class UpDownListener {


    @Autowired
    private IApArticleService articleService;

    @KafkaListener(topics = {"${topic.upDownTopic}"})
    public void handleMessage(ConsumerRecord<String, String> record) {

        //key:文章id
        String key = record.key();
        //value：上架状态
        String value = record.value();
        System.out.println(key + "状态" + value);

        //更新文章的id_down
        LambdaUpdateWrapper<ApArticle> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApArticle::getId, key);
        updateWrapper.set(ApArticle::getIsDown, value.equals("false") ? true : false);
        articleService.update(updateWrapper);
    }

}
